package com.tanhua.dobbo.api;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dobbo.mappers.UserInfoMapper;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.dimain.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 杰哥不要啦~
 * @date 2021/10/27
 */
@DubboService
public class UserInfoApiImpl implements UserInfoApi {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);

    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
       return userInfoMapper.selectById(id);
    }



    @Override
    public Map<Long, UserInfo> finByIds(List<Long> userId, UserInfo info) {
        QueryWrapper qw = new QueryWrapper();
        //1.用户id列表
        qw.in("id",userId);
        //2.添加筛选条件
        if (info.getAge() != null){
            qw.lt( "age",info.getAge());
        }
        if (!StringUtils.isEmpty(info.getGender())){
            qw.eq("gender",info.getGender());
        }
        if (!StringUtils.isEmpty(info.getNickname())){
            qw.eq("nickname",info.getNickname());
        }
        List<UserInfo> list = userInfoMapper.selectList(qw);
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(list, "id");
        return map;
    }
}
